//
//  JJHUD+Extension.swift
//  JJToolSwift
//
//  Created by heihei on 2024/5/11.
//

import Foundation
import MBProgressHUD

extension MBProgressHUD {
    
    /// 添加默认设置
    /// - Parameters:
    ///   - status: 状态类型
    ///   - text: 文字
    func defaultConfig(status: JJHUDStatus, text: String = "") {
        self.bezelView.style = .solidColor
        self.bezelView.color = .hex("#4d4d4d")
        self.contentColor = .white
        self.show(animated: true)
        self.removeFromSuperViewOnHide = true
        self.margin = 20
        self.label.numberOfLines = 0
        if status != .waiting {
            self.label.text = text
            self.label.textColor = .hex("#cbd7e1")
            self.label.font = .boldSystemFont(ofSize: text.isEmpty ? 0 : JJHUDConfig.textSize)
        } else {
            if JJHUDConfig.animateType != .lottie {
                self.label.text = text
                self.label.textColor = .white
                self.removeFromSuperViewOnHide = true
                self.label.font = .boldSystemFont(ofSize: JJHUDConfig.textSize)
            } else {
                self.label.text = ""
                self.label.textColor = .clear
                self.label.font = .systemFont(ofSize: 0)
            }
        }
        self.minSize = CGSize(width: JJHUDConfig.bgViewLong, height: JJHUDConfig.bgViewLong)
    }
    
    /// 加载纯文字
    /// - Parameters:
    ///   - hud: 当前hud
    ///   - text: 文字
    func loadMessage(text: String = "", complete: @escaping() -> Void) {
        DispatchQueue.main.async {
            self.bezelView.color = .black
            self.show(animated: true)
            self.margin = 20
            self.label.text = text
            self.contentColor = .white
            self.minSize = CGSizeZero
            self.mode = .text
            self.removeFromSuperViewOnHide = true
            self.label.font = .boldSystemFont(ofSize: JJHUDConfig.textSize)
            DispatchQueue.main.asyncAfter(deadline: .now() + JJHUDConfig.showTimeInterval) {
                complete()
            }
        }
    }
    
    /// 加载hud图片
    /// - Parameters:
    ///   - hud: 当前hud
    ///   - imageName: 图片名称
    func loadPicture(imageName: String, complete: @escaping() -> Void) {
        let curBundle = Bundle(for: JJHUD.self)
        let image = UIImage(named: imageName, in: curBundle, compatibleWith: nil)
        self.mode = .customView
        let imageView = UIImageView(image: image)
        self.customView = imageView
        self.hide(animated: true, afterDelay: JJHUDConfig.showTimeInterval)
        DispatchQueue.main.asyncAfter(deadline: .now() + JJHUDConfig.showTimeInterval) {
            complete()
        }
    }
    
    /// 加载waiting动画
    /// - Parameters:
    ///   - hud: 当前hud
    ///   - text: 文字
    func loadWaitingAnimated(text: String = "") {
        switch JJHUDConfig.animateType {
        case .lottie:
            self.margin = 0
            let temp = UIView(frame: CGRect(x: 0, y: 0, width: 64, height: 64))
            temp.backgroundColor = .clear
            let animation = LOTAnimationView(name: "data")
            animation.frame = CGRect(x: 0, y: 0, width: 64, height: 64)
            animation.loopAnimation = true
            animation.play { finished in
                // 动画完成后需要处理的事情
            }
            temp.addSubview(animation)
            if !text.isEmpty {
                animation.snp.makeConstraints { make in
                    make.width.height.equalTo(64)
                    make.top.equalToSuperview().offset(3)
                    make.bottom.equalToSuperview().offset(-29)
                    make.left.equalToSuperview().offset(16)
                    make.right.equalToSuperview().offset(-16)
                }
                let loadTitle = UILabel(frame: CGRect(x: 0, y: 0, width: 64, height: 20))
                loadTitle.text = text
                loadTitle.numberOfLines = 0
                loadTitle.textColor = .hex("#CBD7E1")
                loadTitle.font = .boldSystemFont(ofSize: 12)
                loadTitle.textAlignment = .center
                temp.addSubview(loadTitle)
                loadTitle.snp.makeConstraints { make in
                    make.width.equalToSuperview()
                    make.height.equalTo(20)
                    make.left.equalToSuperview()
                    make.top.equalTo(animation.snp.bottom)
                }
            } else {
                animation.snp.makeConstraints { make in
                    make.width.height.equalTo(64)
                    make.top.equalToSuperview().offset(20)
                    make.bottom.equalToSuperview().offset(-10)
                    make.left.equalToSuperview().offset(16)
                    make.right.equalToSuperview().offset(-16)
                }
            }
            self.mode = .customView
            self.customView = temp
            temp.snp.makeConstraints { make in
                make.width.height.equalTo(96)
            }
        case .gif:
            self.margin = 0
            let temp = UIView(frame: CGRect(x: 0, y: 0, width: 64, height: 64))
            temp.backgroundColor = .clear
            let animation = creatGifView()
            animation.frame = CGRect(x: 0, y: 0, width: 64, height: 64)
            animation.startAnimating()
            temp.addSubview(animation)
            if !text.isEmpty {
                animation.snp.makeConstraints { make in
                    make.width.height.equalTo(64)
                    make.top.equalToSuperview().offset(3)
                    make.bottom.equalToSuperview().offset(-29)
                    make.left.equalToSuperview().offset(16)
                    make.right.equalToSuperview().offset(-16)
                }
                let loadTitle = UILabel(frame: CGRect(x: 0, y: 0, width: 64, height: 20))
                loadTitle.text = text
                loadTitle.numberOfLines = 0
                loadTitle.textColor = .hex("#CBD7E1")
                loadTitle.font = .boldSystemFont(ofSize: 12)
                loadTitle.textAlignment = .center
                temp.addSubview(loadTitle)
                loadTitle.snp.makeConstraints { make in
                    make.width.equalToSuperview()
                    make.height.equalTo(20)
                    make.left.equalToSuperview()
                    make.top.equalTo(animation.snp.bottom)
                }
            } else {
                animation.snp.makeConstraints { make in
                    make.width.height.equalTo(64)
                    make.top.equalToSuperview().offset(20)
                    make.bottom.equalToSuperview().offset(-10)
                    make.left.equalToSuperview().offset(16)
                    make.right.equalToSuperview().offset(-16)
                }
            }
            self.mode = .customView
            self.customView = temp
            temp.snp.makeConstraints { make in
                make.width.height.equalTo(96)
            }
        default:
            self.mode = .indeterminate
        }
    }
    
    /// 创建gif，此为示例
    /// - Returns: 返回view
    func creatGifView() -> UIImageView {
        // 创建一个 UIImageView 来显示帧动画
        let imageView = UIImageView(frame: CGRect(x: 0, y: 0, width: 64, height: 64))
        // 创建帧动画的图片数组
        var imageArray: [UIImage] = []
//        for i in 1...39 {
//           let imageName = String(format: "img_loading_%05d", i)
//            if let image:UIImage = String.stringImageNameFromCurrentSize(imageName: imageName, size: CGSize(width: 50, height: 50)) {
//               imageArray.append(image)
//           }
//        }
        for item in 1 ... 3 {
            if let itemImg = UIImage(named: "dropdown_loading_0\(item)") {
                imageArray.append(itemImg)
            }
        }
        // 使用图片数组创建帧动画
        imageView.animationImages = imageArray
        imageView.animationDuration = 0.3 // 设置动画持续时间
        imageView.animationRepeatCount = 0 // 设置动画重复次数，0 表示无限循环
        imageView.contentMode = .scaleAspectFit // 设置 contentMode
        imageView.backgroundColor = UIColor.clear // 设置背景色为透明色
        return imageView
    }
}

public extension UIView {
    
    /// 显示弹窗
    /// - Parameters:
    ///   - status: 状态类型
    ///   - text: 文字
    func show(status: JJHUDStatus, text: String = "") {
        DispatchQueue.main.async {
            let hud = MBProgressHUD(view: self)
            hud.defaultConfig(status: status, text: text)
            self.addSubview(hud)
            // 设置图片
            switch status {
            case .success:
                hud.loadPicture(imageName: "success") {
                    hud.hide(animated: true)
                }
            case .failure:
                hud.loadPicture(imageName: "frustrated") {
                    hud.hide(animated: true)
                }
            case .smile:
                hud.loadPicture(imageName: "smile") {
                    hud.hide(animated: true)
                }
            case .error:
                hud.loadPicture(imageName: "error") {
                    hud.hide(animated: true)
                }
            case .waiting:
                hud.loadWaitingAnimated(text: text)
            case .warn:
                hud.loadPicture(imageName: "warn") {
                    hud.hide(animated: true)
                }
            case .messagePict:
                hud.loadPicture(imageName: "message") {
                    hud.hide(animated: true)
                }
            case .messageNoPict:
                hud.loadMessage(text: text) {
                    hud.hide(animated: true)
                }
            }
        }
    }
    
    /// 隐藏弹窗
    func hideHUD() {
        DispatchQueue.main.async {
            MBProgressHUD.hide(for: self, animated: true)
        }
    }
}

public extension UIViewController {
    
    /// 显示弹窗
    /// - Parameters:
    ///   - status: 状态类型
    ///   - text: 文字
    func show(status: JJHUDStatus, text: String = "") {
        self.view.show(status: status, text: text)
    }
    
    /// 隐藏弹窗
    func hideHUD() {
        DispatchQueue.main.async {
            MBProgressHUD.hide(for: self.view, animated: true)
        }
    }
}
